﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WiimoteLib;
using System.Runtime.InteropServices;

namespace Fitts__law_experiment_with_a_wiimote
{
	public partial class PracticeForm : Form
	{
		[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
		public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);

		private bool dontExit = false;
		private const int MOUSEEVENTF_LEFTDOWN = 0x02;
		private const int MOUSEEVENTF_LEFTUP = 0x04;
		private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
		private const int MOUSEEVENTF_RIGHTUP = 0x10;

		private int count = 40;
		private int Led = 1;
		private int direction = 1;
		private bool AButtonDown = false;

		private Wiimote Mote
		{
			get { return Program.Wiimote; }
		}

		private void UpdateState(object sender, WiimoteChangedEventArgs args)
		{
			if (args.WiimoteState.ButtonState.A)
			{
				if (!AButtonDown)
				{
					mouse_event(MOUSEEVENTF_LEFTDOWN, Cursor.Position.X, Cursor.Position.Y, 0, 0);
					AButtonDown = true;
				}
			}
			else if (AButtonDown)
			{
				mouse_event(MOUSEEVENTF_LEFTUP, Cursor.Position.X, Cursor.Position.Y, 0, 0);
				AButtonDown = false;
			}
		}

		
		public PracticeForm()
		{
			InitializeComponent();
			Mote.SetLEDs(false, false, false, false);
			Mote.WiimoteChanged += UpdateState;
		}

		private void PracticeForm_Load(object sender, EventArgs e)
		{
			TRefreshIR.Start();
		}

		private void TRefreshIR_Tick(object sender, EventArgs e)
		{
			IRSensor IR1 = Mote.WiimoteState.IRState.IRSensors[0];
			IRSensor IR2 = Mote.WiimoteState.IRState.IRSensors[1];
			int safeMarginH = 300;
			int safeMarginV = 300;
			if (IR1.Found)
			{
				Cursor.Position = new System.Drawing.Point(
					(int)((Screen.PrimaryScreen.Bounds.Width + safeMarginH * 2) * (1 - IR1.Position.X) - safeMarginH),
					(int)((Screen.PrimaryScreen.Bounds.Height + safeMarginV * 2) * (IR1.Position.Y) - safeMarginV)
				 );
			}

			if (IR1.Found)
			{
				AimLabel.Visible = false;
			}
			else
				AimLabel.Visible = true;

		}

		private void timer1_Tick(object sender, EventArgs e)
		{
			Led += direction;
			if ((Led == 1) || (Led == 4))
				direction = -direction;
			switch (Led)
			{
				case 1:
					Mote.SetLEDs(true, false, false, false);
					break;
				case 2:
					Mote.SetLEDs(false, true, false, false);
					break;
				case 3:
					Mote.SetLEDs(false, false, true, false);
					break;
				case 4:
					Mote.SetLEDs(false, false, false, true);
					break;
			}
		}

		private void cdTimer_Tick(object sender, EventArgs e)
		{
			count--;
			cdLabel.Text = "The experiment will start in " + count + " seconds";
			if (count == 0)
			{
				dontExit = true;
				Close();
				Experiment1DH form = new Experiment1DH();
				form.Show();
			}
		}

		private void PracticeForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (!dontExit)
			{
				Application.Exit();
			}
		}

		private void openExperimentViewerToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ExperimentViewer form = new ExperimentViewer();
			form.Show();
		}
	}
}
